from typing import List

from fastapi import APIRouter, Depends, Body

from common.pkg.fastapi_util import cbv
from common.utils.depends.common import Page, get_page_info
from core.view import AdminBaseView
from schema.req_schema.attribute_req_schema import AddNewAttributeModel, AddNewAttributeValueModel, \
    UpdateAttributeRequestModel, UpdateAttributeValueRequestModel
from service.attribute_service import AttributeService

router = APIRouter(prefix="/attribute")


@cbv(router=router)
class AttributeView(AdminBaseView):

    @router.get("", description="获取所有的属性信息")
    async def get_attribute_list(self, page_info: Page = Depends(get_page_info)):
        attribute_service = AttributeService(self.mgw, self.redis)
        ok, data_or_code = await attribute_service.get_attribute_tree(page_info)
        return self.unpack_response(ok, data_or_code)

    @router.post("", description="新增属性")
    async def add_new_attribute(self, req_model: AddNewAttributeModel = Body()):
        attribute_service = AttributeService(self.mgw, self.redis)
        ok, data_or_code = await attribute_service.add_new_attribute(req_model)
        return self.unpack_response(ok, data_or_code)

    @router.delete("/{attribute_id:int}", description="删除属性")
    async def delete_attribute(self, attribute_id):
        a_s = AttributeService(self.mgw, self.redis)
        ok, data_or_code = await a_s.delete_attribute(attribute_id)
        return self.unpack_response(ok, data_or_code)

    @router.put("/{attribute_id:int}", description="修改属性名")
    async def update_attribute(self,
                               attribute_id: int,
                               req_model: UpdateAttributeRequestModel = Body(...)):
        ...

    @router.put("/{attribute_id:int}/value", description="属性新增属性值")
    async def add_attribute_value(self,
                                  attribute_id: int,
                                  value_list: List[AddNewAttributeValueModel] = Body(...)):
        a_s = AttributeService(self.mgw, self.redis)
        ok, data_or_code = await a_s.add_attribute_value(attribute_id, value_list)
        return self.unpack_response(ok, data_or_code)

    @router.delete("/{attribute_id:int}/value", description="属性删除属性值")
    async def remove_attribute_value(self,
                                     attribute_id: int,
                                     value_id_list: List[int] = Body(...)
                                     ):
        ...

    @router.put("/{attribute_id:int}/{value_id:int}", description="修改属性的属性值的名称")
    async def update_attribute_value(self,
                                     attribute_id: int,
                                     value_id: int,
                                     req_model: UpdateAttributeValueRequestModel = Body(...)):
        ...

